package com.example.springboot.service.impl;

import com.example.springboot.entity.Customer;
import com.example.springboot.entity.Order;
import com.example.springboot.entity.OrderByUser;
import com.example.springboot.mapper.CustomerMapper;
import com.example.springboot.mapper.OrderMapper;
import com.example.springboot.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cpf
 *
 */
@Service
public class OrderServiceImpl implements IOrderService{
    @Resource
    private OrderMapper orderMapper;

    @Resource
    private CustomerMapper customerMapper;
    @Override
    public List<OrderByUser> queryOrders(Map<String, Object> param) {
        List<OrderByUser> ob = new ArrayList<>();
        //首先查询用户
        List<Customer> list = customerMapper.queryAllInfo(param);
        //通过客户id查询订单
        for (Customer each: list){
            OrderByUser orderByUser = new OrderByUser();
            orderByUser.setOrderUserId(each.getCustomerId());
            orderByUser.setOrderUser(each.getCustomerName());
            orderByUser.setOrderUserPhone(each.getCustomerPhone());
            orderByUser.setOrderUserSex(each.getCustomerSex());
            orderByUser.setOrderUserLevel(each.getCustomerLevel());
            Map<String,Object> query = new HashMap<>();
            query.put("orderUserId",each.getCustomerId());
            List<Order> orderList = orderMapper.queryAllInfo(query);
            orderByUser.setChildren(orderList);
            ob.add(orderByUser);
        }
        return ob;
    }

    /**
    * 通过ID查询单条数据
    *
    * @param id 主键
    * @return 实例对象
    */
    @Override
    public Order queryById(Integer id){
        return this.orderMapper.queryById(id);
    }

    /**
    * 条件查询
    * @Param param 查询条件
    */
    @Override
    public List<Order>queryAllInfo(Map<String,Object> param){
        return this.orderMapper.queryAllInfo(param);
    }
    /**
    * 新增数据
    *
    * @param order 实例对象
    * @return 实例对象
    */
    @Override
    public int insert(Order order){
        //加入逻辑代码
        return this.orderMapper.insert(order);
    }

    /**
    * 修改数据
    *
    * @param order 实例对象
    * @return 实例对象
    */
    @Override
    public int update(Order order){
        //加入逻辑代码
        return this.orderMapper.update(order);
    }

    /**
    * 通过主键删除数据
    *
    * @param ids 主键list
    * @return 是否成功
    */
    @Override
    public int deleteByIds(List<Integer> ids){
        return this.orderMapper.deleteByIds(ids);
    }

    /**
    *查询结果数
    */
    @Override
    public long count(Map<String,Object> param){
        return this.orderMapper.count(param);
    }

    /**
    * 批量插入
    */
    @Override
    public int insertBatch(List<Order> entities){
        return this.orderMapper.insertBatch(entities);
    }
}
